package com.lc.luzhengtang.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.activity.LoginActivity;
import com.lc.luzhengtang.activity.ShopDetailActivity;
import com.lc.luzhengtang.bean.CommenBean;
import com.lc.luzhengtang.conn.Conn;
import com.lc.luzhengtang.conn.ConnAddShop;
import com.lc.luzhengtang.conn.ConnIndexShopList;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.List;


/**
 * Created by Administrator on 2017/1/22.
 */

public class IndexAdapter extends BaseQuickAdapter<ConnIndexShopList.Info.NewListContent, BaseViewHolder> {

    TextView textView;
//加入购物车的图片
    ImageView img_add_purchase;
    public IndexAdapter(List<ConnIndexShopList.Info.NewListContent> data) {

        super(R.layout.item_index, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final ConnIndexShopList.Info.NewListContent item) {
        ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) helper.convertView);
        helper.setText(R.id.text_nicheng, item.title);
        helper.setText(R.id.text_money, "¥" + item.sales);
        helper.setText(R.id.text_xiaoliang, "销量：" + item.volume);
        helper.setText(R.id.text_oldmoney, "¥" + item.market_price);
        //  helper.setImageResource(R.id.img_index, Glide.with(mContext).load("").into(imageView) );
        Glide.with(mContext).load(Conn.HOST3 + item.picurl).crossFade().error(R.mipmap.zhanwei).into((ImageView) helper.getView(R.id.img_index));
//        helper.addOnClickListener(R.id.img_add_purchase);
        int position = helper.getPosition();
        if (position % 2 == 1) {
            ScaleScreenHelperFactory.getInstance().loadViewMargin(helper.convertView.findViewById(R.id.ll_content), 0, 23, 30, 0);
        } else {
            ScaleScreenHelperFactory.getInstance().loadViewMargin(helper.convertView.findViewById(R.id.ll_content), 0, 23, 24, 0);
        }
        img_add_purchase=helper.getView(R.id.img_add_purchase);
        img_add_purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(BaseApplication.basePreferences.readUid())){
                    mContext.startActivity(new Intent(mContext, LoginActivity.class));
                }else {

                    new ConnAddShop(item.id + "", BaseApplication.basePreferences.readUid(), "1", "盒", item.sales, new AsyCallBack<CommenBean>() {

                        @Override
                        public void onStart(int type) throws Exception {
                            super.onStart(type);
                        }

                        @Override
                        public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                            super.onSuccess(toast, type, o, commenBean);
                            //  Toast.makeText(getActivity(), "成功加入购物车", Toast.LENGTH_SHORT).show();
                            LovelyToast.makeText(mContext, "成功加入购物车", 0, LovelyToast
                                    .SUCCESS, LovelyToast.SCALE);
                        }

                        @Override
                        public void onFail(String toast, int type) throws Exception {
                            super.onFail(toast, type);
                        }

                        @Override
                        public void onEnd(String toast, int type) throws Exception {
                            super.onEnd(toast, type);
                            //可能需要移除之前添加的布局

                        }
                    }

                    ).execute(mContext, false);
                }
            }
        });

       /* mIndexAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {


                new ConnAddShop(info.data.get(position).id + "", BaseApplication.basePreferences.readUid(), "1", "盒", info.data.get(position).sales, new AsyCallBack<CommenBean>() {

                    @Override
                    public void onStart(int type) throws Exception {
                        super.onStart(type);
                    }

                    @Override
                    public void onSuccess(String toast, int type, Object o, CommenBean commenBean) throws Exception {
                        super.onSuccess(toast, type, o, commenBean);
                        //  Toast.makeText(getActivity(), "成功加入购物车", Toast.LENGTH_SHORT).show();
                        LovelyToast.makeText(getActivity(), "成功加入购物车", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

                    @Override
                    public void onFail(String toast, int type) throws Exception {
                        super.onFail(toast, type);
                    }

                    @Override
                    public void onEnd(String toast, int type) throws Exception {
                        super.onEnd(toast, type);
                        //可能需要移除之前添加的布局

                    }
                }

                ).execute(getActivity(), false);
            }


        });*/



        textView = helper.getView(R.id.text_oldmoney);textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        View view = helper.getConvertView();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              /*  //   WebActivity.runActivity(mContext, model.desc, model.url);
                Intent intent = new Intent();
                intent.putExtra("goodstype_id", item + "");
                intent.setClass(mContext, ConsumptionActiivty.class);
                mContext.startActivity(intent);*/

                Intent intent = new Intent();
                intent.putExtra("goods_id", item.id+"");
                intent.putExtra("img_url",item.picurl);
                intent.setClass(mContext, ShopDetailActivity.class);
                mContext.startActivity(intent);
            }
        });
    }


}
